package com.wmlive.hhvideo.heihei.websocket;

import com.google.gson.Gson;
import com.wmlive.hhvideo.heihei.beans.message.IMMessageBean;
import com.wmlive.hhvideo.heihei.message.AbstractMessage;
import com.wmlive.hhvideo.heihei.message.IMMessage;
import com.wmlive.hhvideo.heihei.message.IMMessageDispatcher;
import com.wmlive.hhvideo.utils.KLog;
import com.wmlive.hhvideo.utils.ThreadManager;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * websocket消息分发
 * Created by kangzhen on 2017/7/6.
 */

public class MessageDistribute {
    private String TAG = "MessageDistribute";
    private static MessageDistribute mMessageDistribute;

    public static MessageDistribute getInstance() {
        if (mMessageDistribute == null) {
            synchronized (MessageDistribute.class) {
                if (mMessageDistribute == null) {
                    mMessageDistribute = new MessageDistribute();
                }
            }
        }
        return mMessageDistribute;
    }

    private MessageDistribute() {
    }

    /**
     * 发送消息
     * @param json
     */
    public void sendMessage(final String json) {
        ThreadManager.getInstance().execute(new Runnable() {

            @Override
            public void run() {
                try {
                    JSONArray array = new JSONArray(json);
                    for (int i = 0; i < array.length(); i++) {
                        paserJsonItem(array.getJSONObject(i));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    KLog.e(TAG,"---------Exception----------e:"+e.getMessage());
                }
            }
        });
    }

    /**
     * 处理消息
     * @param obj
     */
    public void paserJsonItem(JSONObject obj) {
        try {
            String classifyType = obj.getString("classify");
            JSONObject mesOjb = obj.optJSONObject("message");
            KLog.i(TAG,"-----------paserJsonItem-------:"+mesOjb.toString());
            switch (classifyType) {
                case AbstractMessage.MESSAGE_TYPE_IM:
                    paserIMInfo(mesOjb);
                    break;
                default:
                    KLog.i(TAG, "case type : not fond default");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            KLog.i(TAG, e.getMessage());
        }
    }

    /**
     * 解析IM 信息
     *
     * @param o
     */
    public synchronized void paserIMInfo(JSONObject o) {
        try {
            Gson mGson = new Gson();
            IMMessageBean mImMsgBean = null ;
            //IM中消息体部分
            KLog.i(TAG, "---------paserIMInfo--------:"+o.toString());
            if (o!=null ) {
                mImMsgBean = mGson.fromJson(o.toString(), IMMessageBean.class);
            }
            if (mImMsgBean != null) {
                KLog.i(TAG,"----paserIMInfo-----------IMMessageDispatcher-----start-");
                String strFromUserId = "";
                String strToUserId ="";
                if(mImMsgBean!=null && mImMsgBean.getFrom_user()!=null){
                    strFromUserId =String.valueOf( mImMsgBean.getFrom_user().getId());
                }
                if(mImMsgBean!=null && mImMsgBean.getTo_user()!=null){
                    strToUserId =String.valueOf( mImMsgBean.getTo_user().getId());
                }
                IMMessage imMessage = new IMMessage(mImMsgBean.getMsg_type(),strFromUserId,strToUserId,o.toString());
                IMMessageDispatcher.getInstance().addIMMessgeToQuene(imMessage);
                KLog.i(TAG,"----paserIMInfo-----------IMMessageDispatcher-----end-");
            } else {
                KLog.e(TAG, "--------paserIMInfo--------- null ");
            }
        } catch (Exception e) {
            KLog.e(TAG, "-------Exception----------" + e.getLocalizedMessage());
        }
    }

}
